<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Car extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'car';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'power_text', 'track_text'
    ];

    public function user()
    {
        return $this->belongsTo('User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getPowerList()
    {
        return ['1' => '汽油', '2' => '柴油', '3' => '油电混合'];
    }
    

    public function getPowerTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['power_type'] ?? '');
        $list = $this->getPowerList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTrackList()
    {
        return ['1' => '微型', '2' => '轻型', '3' => '中型', '4' => '重型'];
    }

    public function getTrackTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['track_type'] ?? '');
        $list = $this->getTrackList();
        return $list[$value] ?? '';
    }

    public function carbrand()
    {
        return $this->belongsTo('carBrand', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function driver()
    {
        return $this->belongsTo('Driver', 'driver_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
